package com.test.io;

import java.io.*;
import java.nio.CharBuffer;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/3/18 9:51
 */
public class CharIo {
    public static void main(String[] args) throws IOException {
//        FileDescriptor fileDescriptor = new FileDescriptor();
//        File file = new File()
        CharBuffer buffer = CharBuffer.allocate(2); //存两个字符，而不是字节。已测试
        InputStreamReader reader = new InputStreamReader(new FileInputStream(FileInfo.ChineseFile),"GBK");
        BufferedReader br = new BufferedReader(reader);
        //reader.read()
        br.read();
        br.close();
        while (reader.read(buffer) != -1) {
            buffer.flip();
            System.out.println(buffer);
            buffer.clear();
        }
    }

    public void readByInputStreamReader() {

    }

    public void readByDirectFileReader() throws IOException {
        FileReader reader = new FileReader(FileInfo.ChineseFile);
        System.out.println(reader.getEncoding());
        CharBuffer buffer = CharBuffer.allocate(2);
        // Charset gbk = Charset.forName("GBK");
        while (reader.read(buffer) != -1) {

            buffer.flip();
            System.out.println(buffer);
            buffer.clear();
        }
        int size = reader.read(buffer);
        buffer.flip();
        System.out.println(size);
        // reader
        // gbk.decode(buffer)
        //System.out.println(new String(buffer));\
        char[] chinese = new char[2];
        buffer.get(chinese);
        String s = new String(chinese);
        System.out.println(s);
        System.out.println(buffer);;
    }
}
